<?php
// +---------------------------------------------------------------------+
// | NiuCloud | [ WE CAN DO IT JUST NiuCloud ]                |
// +---------------------------------------------------------------------+
// | Copy right 2019-2029 www.niucloud.com                          |
// +---------------------------------------------------------------------+
// | Author | NiuCloud <niucloud@outlook.com>                       |
// +---------------------------------------------------------------------+
// | Repository | https://github.com/niucloud/framework.git          |
// +---------------------------------------------------------------------+

namespace app\model\system;

use app\model\BaseModel;
use think\facade\Db;

class Upgrade extends BaseModel
{
	private $url = "https://www.niushop.com.cn/auth/Upgrade/%s";
	private $cert;
	
	public function __construct()
	{
		if (file_exists('cert.key')) {
			$this->cert = file_get_contents('cert.key');
		}
	}
	
	/**
	 * 获取可升级的版本信息
	 */
	public function getUpgradeVersion()
	{
		$url = sprintf($this->url, 'getUpgradeVersion');
		$post_data = [
			'cert' => $this->cert,
			'version_release' => SYS_RELEASE,
			'module_mark' => SYS_VERSION
		];
		$re = $this->doPost($url, $post_data);
		$re = json_decode($re, true);
		return $re;
	}
	
	/**
	 * 在线下载文件
	 * @param $param
	 */
	public function download($param)
	{
		//验证授权
		$file = $param["file"];//下载文件路径
		$url = sprintf($this->url, 'download');
		$data = array(
			"module_mark" => $param["module_mark"],
			"version_release" => $param["version_release"],
			"file" => $file,
			"token" => $param["token"]
		);
		$result = $this->doPost($url, $data);//授权
		if (empty($result))
			return $this->error();
		
		$result = json_decode($result, true);
		return $result;
	}
	
	/**
	 * 获取版本详情
	 */
	public function getUpgradeInfo($last_version_release)
	{
		$url = sprintf($this->url, 'getUpgradeInfo');
		$post_data = [
			'cert' => $this->cert,
			'version_release' => SYS_RELEASE,
			'module_mark' => SYS_VERSION,
			'last_version_release' => $last_version_release
		];
		$re = $this->doPost($url, $post_data);
		$re = json_decode($re, true);
		return $re;
	}
	
	/**
	 * 获取授权信息
	 * @return bool|mixed|string
	 */
	public function authInfo()
	{
		$url = sprintf($this->url, 'authInfo');
		$post_data = [
			'cert' => $this->cert
		];
		$re = $this->doPost($url, $post_data);
		$re = json_decode($re, true);
		return $re;
	}
	
	/**
	 * 获取最新的版本信息
	 * @return bool|string
	 */
	public function getLatestVersion()
	{
		$url = sprintf($this->url, 'getLatestVersion');
		$post_data = [
			'cert' => $this->cert
		];
		$re = $this->doPost($url, $post_data);
		$re = json_decode($re, true);
		return $re;
	}
	
	/**
	 * 文件对比
	 * @param $data
	 * @return bool|mixed|string
	 */
	public function getTowDevFiles($data)
	{
		$url = sprintf($this->url, 'fileContrast');
		$post_data = [
			'cert' => $this->cert,
			'module_release' => SYS_RELEASE,
			'module_mark' => SYS_VERSION,
			'files' => json_encode($data, true),
		];
		$re = $this->doPost($url, $post_data);
		$re = json_decode($re, true);
		
		return $re;
	}
	
	/**
	 * 查询所有表
	 */
	public function getDatabaseList()
	{
		$databaseList = Db::query("SHOW TABLE STATUS");
		return $databaseList;
	}
	
	
	/**
	 * post 服务器请求
	 */
	private function doPost($post_url, $post_data)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $post_url);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		
		if ($post_data != '' && !empty($post_data)) {
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		}
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}
}